package xxh;

import javax.swing.*;
import java.awt.*;
import java.sql.*;

class BorrowUI extends JPanel {
    private Connection connection;
    private JPanel userPanel;
    private JLabel lblUserNo;
    private JTextField txtUserNo;
    private JButton btnSubmit;
    private JPanel bookPanel;
    private JScrollPane scrollPane;  // 添加滚动面板

    BorrowProcess brp;

    public BorrowUI(String Uno) throws ClassNotFoundException, SQLException {

        // 设置整体布局
        setLayout(new BorderLayout());

        // 用户信息部分
        userPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 20));

        lblUserNo = new JLabel("用户编号:");
        lblUserNo.setFont(new Font("微软雅黑", Font.PLAIN, 16)); // 设置字体样式和大小
        lblUserNo.setForeground(new Color(45, 45, 45)); // 设置文字颜色

        txtUserNo = new JTextField(15);
        txtUserNo.setText(Uno);
        txtUserNo.setEditable(false);
        txtUserNo.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        txtUserNo.setForeground(new Color(50, 50, 50));
        txtUserNo.setBackground(Color.WHITE);
        txtUserNo.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200), 1));  // 添加边框

        userPanel.add(lblUserNo);
        userPanel.add(txtUserNo);
        add(userPanel, BorderLayout.NORTH);

        // 书籍选择区域
        bookPanel = new JPanel();
        bookPanel.setLayout(new GridLayout(0, 3, 15, 15)); // 设置每行3本书，适当增加间距

        // 添加滚动面板
        scrollPane = new JScrollPane(bookPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);  // 设置垂直滚动条
        scrollPane.setPreferredSize(new Dimension(600, 300));  // 设置滚动区域大小
        add(scrollPane, BorderLayout.CENTER);  // 将滚动面板添加到界面中

        // 借阅按钮部分
        btnSubmit = new JButton("提交借阅");
        btnSubmit.setFont(new Font("微软雅黑", Font.BOLD, 18));
        btnSubmit.setBackground(new Color(0, 123, 255));  // 蓝色按钮
        btnSubmit.setForeground(Color.WHITE);  // 白色字体
        btnSubmit.setFocusPainted(false);  // 去除按钮焦点框
        btnSubmit.setBorder(BorderFactory.createLineBorder(new Color(0, 105, 217), 2));  // 边框颜色
        btnSubmit.setPreferredSize(new Dimension(150, 45)); // 设置按钮大小
        btnSubmit.addActionListener(e -> borrowBooks(Uno));

        JPanel buttonPanel = new JPanel();
        buttonPanel.setBackground(new Color(248, 249, 250));  // 设置背景颜色
        buttonPanel.add(btnSubmit);
        add(buttonPanel, BorderLayout.SOUTH);

        // 加载书籍信息
        brp = new BorrowProcess(bookPanel);
    }

    private void borrowBooks(String userNo) {
        for (Component comp : bookPanel.getComponents()) {
            if (comp instanceof BookPanel) {
                BookPanel bookPanel = (BookPanel) comp;
                if (bookPanel.isSelected()) {
                    try {
                        String bookNo = bookPanel.getBookNo();
                        if (brp.borrowBook(userNo, bookNo)) {
                            JOptionPane.showMessageDialog(this, "借书成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
                        } else {
                            JOptionPane.showMessageDialog(this, "借书失败，库存不足.", "错误", JOptionPane.ERROR_MESSAGE);
                        }
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(this, "操作失败，请重试!", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }
    }
}
